{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hamiltonian Simulation\n",
    "\n",
    "The hamiltonian that we simulate is $$H=(a Z_0+b Z_1+c Z_2 +d Z_3)^2$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyqpanda import *\n",
    "from pyqpanda.utils import *\n",
    "from pyqpanda.Hamiltonian import PauliOperator, create_x_driver_hamiltonian\n",
    "from pyqpanda.Algorithm.hamiltonian_simulation import *\n",
    "\n",
    "from numpy import *\n",
    "from pylab import *\n",
    "from matplotlib import pyplot as plt\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "from copy import deepcopy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## An instance"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "RZ(): incompatible function arguments. The following argument types are supported:\n    1. (arg0: Qubit, arg1: float) -> QGate\n\nInvoked with: <pyqpanda.pyQPanda.Qubit object at 0x00000260645E0F48>, (-0.03125+0j)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-3-356f1a728a03>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m     15\u001b[0m \u001b[0mshots\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m100\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mresult\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0madiabatic_simulation_with_configuration\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqn_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mqn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mHp_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mHp\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mHd_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mHd\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mstep_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mstep\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mslices_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mslices\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mt_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mshots_\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mshots\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     18\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     19\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mget_fidelity\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mshots\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtarget_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pyqpanda-2.1-py3.6.egg\\pyqpanda\\Algorithm\\hamiltonian_simulation.py\u001b[0m in \u001b[0;36madiabatic_simulation_with_configuration\u001b[1;34m(qn_, Hp_, Hd_, step_, slices_, t_, shots_)\u001b[0m\n\u001b[0;32m    121\u001b[0m         \u001b[0mHt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mHp_\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mstep_\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m+\u001b[0m\u001b[0mHd_\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep_\u001b[0m\u001b[1;33m-\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mstep_\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    122\u001b[0m         \u001b[0mHt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0marrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 123\u001b[1;33m         \u001b[0mprog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulate_hamiltonian\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mq\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mHt\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mt_\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mstep_\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mslices\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mslices_\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    124\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    125\u001b[0m     \u001b[0mprog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmeas_all\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mq\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pyqpanda-2.1-py3.6.egg\\pyqpanda\\Algorithm\\hamiltonian_simulation.py\u001b[0m in \u001b[0;36msimulate_hamiltonian\u001b[1;34m(qubit_list, pauliOperator, t, slices)\u001b[0m\n\u001b[0;32m     94\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mop\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mpauliOperator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mops\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     95\u001b[0m             prog.insert(simulate_one_term(qubit_list,op,\n\u001b[1;32m---> 96\u001b[1;33m                 \u001b[0mpauliOperator\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mops\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mop\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m/\u001b[0m\u001b[0mslices\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     97\u001b[0m             ))\n\u001b[0;32m     98\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pyqpanda-2.1-py3.6.egg\\pyqpanda\\Algorithm\\hamiltonian_simulation.py\u001b[0m in \u001b[0;36msimulate_one_term\u001b[1;34m(qubit_list, hamiltonian_term, coef, t)\u001b[0m\n\u001b[0;32m     71\u001b[0m             \u001b[0mactual_qlist\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqubit_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0msingle_term\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     72\u001b[0m     \u001b[0mprog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtransform\u001b[0m\u001b[1;33m)\u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 73\u001b[1;33m         \u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulate_z_term\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mactual_qlist\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcoef\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[0;31m\\\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     74\u001b[0m         \u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtransform\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdagger\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     75\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pyqpanda-2.1-py3.6.egg\\pyqpanda\\Algorithm\\hamiltonian_simulation.py\u001b[0m in \u001b[0;36msimulate_z_term\u001b[1;34m(qubit_list, coef, t)\u001b[0m\n\u001b[0;32m     34\u001b[0m             \u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mparity_check_circuit\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqubit_list\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     35\u001b[0m     \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 36\u001b[1;33m         \u001b[0mprog\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minsert\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mRZ\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqubit_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mcoef\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mt\u001b[0m\u001b[1;33m*\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     37\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     38\u001b[0m     \u001b[1;32mreturn\u001b[0m \u001b[0mprog\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\Anaconda3\\lib\\site-packages\\pyqpanda-2.1-py3.6.egg\\pyqpanda\\pywrapper.py\u001b[0m in \u001b[0;36mRZ\u001b[1;34m(qubit, angle)\u001b[0m\n\u001b[0;32m    266\u001b[0m     \u001b[0mQubit\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mdouble\u001b[0m \u001b[1;33m->\u001b[0m \u001b[0mQGate\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    267\u001b[0m     \"\"\"\n\u001b[1;32m--> 268\u001b[1;33m     \u001b[1;32mreturn\u001b[0m \u001b[0mpyQPanda\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mRZ\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mqubit\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mangle\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    269\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    270\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mCNOT\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcontrol_qubit\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mtarget_qubit\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: RZ(): incompatible function arguments. The following argument types are supported:\n    1. (arg0: Qubit, arg1: float) -> QGate\n\nInvoked with: <pyqpanda.pyQPanda.Qubit object at 0x00000260645E0F48>, (-0.03125+0j)"
     ]
    }
   ],
   "source": [
    "Hp={'Z0':1,'Z1':2,'Z2':3,'Z3':4,'Z4':5,'Z5':7}\n",
    "Hp=PauliOperator(Hp)\n",
    "Hp=Hp*Hp\n",
    "\n",
    "#fetch number of qubit from hamiltonian\n",
    "qn=Hp.get_qubit_count()\n",
    "\n",
    "#create the corresponding Sigma_X driver hamiltonian\n",
    "Hd=create_x_driver_hamiltonian(qn)\n",
    "\n",
    "target_result=['111010','000101','101001','010110']\n",
    "step=200\n",
    "slices=16\n",
    "t=100\n",
    "shots=100\n",
    "\n",
    "result=adiabatic_simulation_with_configuration(qn_=qn, Hp_=Hp,Hd_=Hd,step_=step,slices_=slices,t_=t,shots_=shots)\n",
    "print(result)\n",
    "print(get_fidelity(result,shots,target_result))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# configure scanning params\n",
    "step_start=1\n",
    "step_end=30\n",
    "step_step=8\n",
    "\n",
    "step_linspace=range(step_start,step_end,step_step)\n",
    "\n",
    "slice_start=1\n",
    "slice_end=30\n",
    "slice_step=8\n",
    "\n",
    "slice_linspace=range(slice_start,slice_end,slice_step)\n",
    "\n",
    "t_start=0.5\n",
    "t_end=2.0\n",
    "t_count=6\n",
    "\n",
    "t_linspace=linspace(t_start,t_end,t_count)\n",
    "\n",
    "meshgrid_slice, meshgrid_step=meshgrid(slice_linspace,step_linspace)\n",
    "\n",
    "# number of shots\n",
    "shots=1000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 't_linspace' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-4-7bb2fe28f80b>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mfidelity_list\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mi_t\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mt\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mt_linspace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[0mmeshgrid_fidelity\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdeepcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmeshgrid_slice\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi_step\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mstep\u001b[0m \u001b[1;32min\u001b[0m \u001b[0menumerate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstep_linspace\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 't_linspace' is not defined"
     ]
    }
   ],
   "source": [
    "fidelity_list=list()\n",
    "\n",
    "for i_t,t in enumerate(t_linspace):\n",
    "    meshgrid_fidelity=deepcopy(meshgrid_slice)\n",
    "    for i_step,step in enumerate(step_linspace):\n",
    "        for i_slices,slices in enumerate(slice_linspace):\n",
    "            result=simulate_with_configuration(Hp_=Hp, Hd_=Hd, step_=step,\n",
    "                                        slices_=slices, t_=t, shots_=shots )\n",
    "            fidelity=get_fidelity(result,shots)\n",
    "            print('t=',t ,'step=',step,'slice=',slices,'Fidelity=',fidelity)\n",
    "            meshgrid_fidelity[i_slices,i_step]=fidelity\n",
    "            \n",
    "    #save the result\n",
    "    fidelity_list.append(meshgrid_fidelity)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "67521\n"
     ]
    }
   ],
   "source": [
    "def simulate_with_configuration_str(Hp_, Hd_, step_, slices_, t_ ):\n",
    "    init()\n",
    "    prog=QProg()\n",
    "    q=qAlloc_many(6)\n",
    "    c=cAlloc_many(6)\n",
    "    prog.insert(single_gate_apply_to_all(gate=X, qubit_list=q))\n",
    "    prog.insert(single_gate_apply_to_all(gate=H, qubit_list=q))\n",
    "\n",
    "    for i in range(step_+1):\n",
    "        Ht=Hp_.mul(i/step_)+Hd_.mul((step_-i)/step_)\n",
    "        Ht.arrange()\n",
    "        prog.insert(simulate_hamiltonian(q,Ht,t=t_,slices=slices_))\n",
    "\n",
    "    prog.insert(meas_all(q,c))\n",
    "    string=to_qrunes(prog)\n",
    "    finalize()\n",
    "    return string\n",
    "\n",
    "prog_str=simulate_with_configuration_str(Hp,Hd,20,20,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "s=prog_str.split('\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "print(len(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "start =time.clock()\n",
    "step=100\n",
    "slices=100\n",
    "\n",
    "prog_str=simulate_with_configuration_str(Hp,Hd,step,slices,1)\n",
    "s=prog_str.split('\\n')\n",
    "print('code length:',len(s))\n",
    "\n",
    "\n",
    "result=result=simulate_with_configuration(Hp_=Hp, Hd_=Hd, step_=step,\n",
    "                                        slices_=slices, t_=10, shots_=shots )\n",
    "\n",
    "fidelity=get_fidelity(result,shots)\n",
    "print(fidelity)\n",
    "\n",
    "end = time.clock()\n",
    "print('Elasped time: %s Seconds'%(end-start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
